package com.cheboohui.iqs.taiping.direct.order;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.spring.entity.price.request.ConfirmInsured;
import com.cheboohui.iqs.spring.entity.price.response.ConfirmInsuredResult;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;
import com.cheboohui.iqs.taiping.direct.v.V03Request;

/**
 * 确认投保
 * 
 * @author xielonghai
 *
 */
public class ConfirmPolicy {

	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(ConfirmPolicy.class);

	/**
	 * 确认投保
	 * 
	 * @param confirmInsured
	 * @return
	 */
	public static ConfirmInsuredResult confirm(ConfirmInsured confirmInsured) {
		log.info(Config.value + "--进入确认投保接口....开始访问V03接口开始...");

		ResTaiPing v03 = V03Request.requestV03("", getPraamten(confirmInsured));

		if (!v03.isState()) {
			log.error(Config.value + "--进入确认投保接口....访问V03接口失败...:" + v03.getMsg());
			return new ConfirmInsuredResult(v03.getError(), v03.getMsg());
		}

		log.info(Config.value + "--进入确认投保接口....访问V03接口成功-开始处理...");

		JSONArray arr = v03.getResult().getJSONArray("risk");
		boolean next = false;
		for (int i = 0; i < arr.size(); i++) {
			if (arr.getJSONObject(i).getString("riskCode").equals("BZ")) {
				confirmInsured.getPolicy().setTraficInsuredNo(arr.getJSONObject(i).getString("proposalNo"));
				if ("1".equals(arr.getJSONObject(i).getString("underWriteInd"))
						|| "3".equals(arr.getJSONObject(i).getString("underWriteInd"))) {
					next = true;
				}
			} else if (arr.getJSONObject(i).getString("riskCode").equals("BS")) {
				confirmInsured.getPolicy().setBusinessInsuredNo(arr.getJSONObject(i).getString("proposalNo"));
				if ("1".equals(arr.getJSONObject(i).getString("underWriteInd"))
						|| "3".equals(arr.getJSONObject(i).getString("underWriteInd"))) {
					next = true;
				}
			}
		}

		if (next) {
			confirmInsured.getPolicy().setCode("02");
			confirmInsured.getPolicy().setCodeValue("确认投保通过");
			log.info(Config.value + "--进入确认投保接口....访问V03接口成功-处理完成...");
			return new ConfirmInsuredResult(confirmInsured.getPolicy());
		} else {
			return new ConfirmInsuredResult(ErrorNews.L03, "太平直连确认投保接口审核标志出现未知");
		}

	}

	/**
	 * 转化V03接口请求JSON数据
	 * 
	 * @param confirmInsured
	 * @return
	 */
	private static JSONObject getPraamten(ConfirmInsured confirmInsured) {
		/** 订单信息 **/
		JSONObject order = new JSONObject();
		order.put("orderNo", confirmInsured.getPolicy().getOrderNo());// 订单号-Y

		/** 车主信息（carOwner） **/
		JSONObject carOwner = new JSONObject();
		carOwner.put("name", confirmInsured.getOwner().getName());// 车主姓名-Y
		carOwner.put("identifyNumber", confirmInsured.getOwner().getIdCard());// 车主身份证号码-Y

		/** 投保人信息（applicant */
		JSONObject applicant = new JSONObject();
		applicant.put("appliName", confirmInsured.getApplicant().getName());// 投保人名称-Y
		applicant.put("identifyNumber", confirmInsured.getApplicant().getIdCard());// 投保人证件号码-Y
		applicant.put("mobilePhone", confirmInsured.getApplicant().getPhone());// 移动电话-Y
		applicant.put("email", StringUtil.isNulls(confirmInsured.getApplicant().getEmail()) ? ""
				: confirmInsured.getApplicant().getEmail());// email--北京地区需要传送
		applicant.put("appliAddress", confirmInsured.getApplicant().getAddress());// 投保人地址-Y

		/*** 被保险人信息（insured） */
		JSONObject insured = new JSONObject();
		insured.put("insuredName", confirmInsured.getInsured().getName());// 被保人名称-Y
		insured.put("identifyNumber", confirmInsured.getInsured().getIdCard());// 被保人证件号码-Y
		insured.put("insuredMobilePhone", confirmInsured.getInsured().getPhone());// 移动电话-Y
		insured.put("insuredAddress", StringUtil.isNulls(confirmInsured.getInsured().getAddress())
				? confirmInsured.getApplicant().getAddress() : confirmInsured.getInsured().getAddress());// 被保人地址-Y

		/** 配送信息（dispatch） */
		JSONObject dispatch = new JSONObject();
		dispatch.put("name", confirmInsured.getDispatch().getName());// 收件人名称
		dispatch.put("address", confirmInsured.getDispatch().getAddress());// 收件人地址-Y
		dispatch.put("mobilePhone", confirmInsured.getDispatch().getPhone());// 移动电话-Y
		dispatch.put("officePhone", "");// 联系电话

		JSONObject param = new JSONObject();
		param.put("order", order);
		param.put("carOwner", carOwner);
		param.put("applicant", applicant);
		param.put("insured", insured);
		param.put("dispatch", dispatch);
		return param;
	}
}
